from datetime import datetime

from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields

from config.enums import BusinessType
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.business.wlkts_service import WlktsService
from sqlalchemy.ext.asyncio import AsyncSession
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.data_scope import GetDataScope
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.service.login_service import LoginService
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

from module_admin.entity.vo.business.wlkts_vo import WlktsModel, WlktsPageQueryModel, AddWlktsModel

wlktsController = APIRouter(prefix='/wlkts', dependencies=[Depends(LoginService.get_current_user)])


@wlktsController.get('/list', response_model=PageResponseModel,
                     dependencies=[Depends(CheckUserInterfaceAuth('business:wlkts:list'))])
async def get_wlkts_list(
        request: Request,
        wlkts_page_query: WlktsPageQueryModel = Depends(WlktsPageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
        data_scope_sql: str = Depends(GetDataScope('Wlkts', 'db', 'user_id', 'duty_dept'))
):
    """
    获取往来款提示信息列表
    """
    wlkts_query_result = await WlktsService.get_wlkts_list_services(query_db, wlkts_page_query, data_scope_sql,
                                                                    is_page=True)
    return ResponseUtil.success(model_content=wlkts_query_result)


@wlktsController.post('', dependencies=[Depends(CheckUserInterfaceAuth('business:wlkts:add'))])
@ValidateFields(validate_model='add_wlkts')
@Log(title='往来款提示记录新增', business_type=BusinessType.INSERT)
async def add_wlkts(
        request: Request,
        add_wlkts: AddWlktsModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    add_wlkts.create_by = current_user.user.user_name
    add_wlkts.create_time = datetime.now()
    add_wlkts.update_by = current_user.user.user_name
    add_wlkts.update_time = datetime.now()
    add_wlkts_result = await WlktsService.add_wlkts_services(query_db, add_wlkts)
    logger.info(add_wlkts_result)

    return ResponseUtil.success(msg=add_wlkts_result.message)


@wlktsController.put('', dependencies=[Depends(CheckUserInterfaceAuth('business:wlkts:edit'))])
@ValidateFields(validate_model='edit_wlkts')
@Log(title='往来款提示记录修改', business_type=BusinessType.UPDATE)
async def edit_wlkts(
        request: Request,
        edit_wlkts: AddWlktsModel,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    修改往来款提示信息
    """
    edit_wlkts.update_by = current_user.user.user_name
    edit_wlkts.update_time = datetime.now()
    edit_wlkts_result = await WlktsService.edit_wlkts_services(query_db, edit_wlkts)
    logger.info(edit_wlkts_result.message)

    return ResponseUtil.success(msg=edit_wlkts_result.message)


@wlktsController.delete('/{id}', dependencies=[Depends(CheckUserInterfaceAuth('business:wlkts:delete'))])
@Log(title='往来款提示记录删除', business_type=BusinessType.DELETE)
async def delete_wlkts(
        request: Request,
        id: int,
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    """
    删除往来款提示信息
    """
    delete_result = await WlktsService.delete_wlkts_services(query_db, id, current_user.user.user_name)
    logger.info(delete_result.message)

    return ResponseUtil.success(msg=delete_result.message)


